<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Book_Picture extends Controller{

public function action_index()
{
}

public function action_list()
{
	$picture = ORM::factory('picture');
	if ($_GET)
	{
		$booklet_id = arr::get($_GET, 'booklet_id');
		$page = arr::get($_GET, 'page');
		$num_in_page = arr::get($_GET, 'num_in_page');
		if ($booklet_id)
		{
			$picture->where('booklet_id', '=', $booklet_id);
		}
	}
	$query_result = $picture->find_all();
	if (isset($page) AND isset($num_in_page))
	{
		$result = Book_Picture::instance()->create_picture_list(array_slice($query_result->as_array(), ($page - 1) * $num_in_page, $num_in_page));
	}
	else
	{
		$result = Book_Picture::instance()->create_picture_list($query_result->as_array());
	}
	if (isset($booklet_id))
	{
		Book_Picture::instance()->_add_browser_count($booklet_id);
	}
	json_Output::write($result);
}

public function action_rand()
{
	$rand_num = arr::get($_GET, 'num');
	$category_id = arr::get($_GET, 'category_id');
	$lock = arr::get($_GET, 'lock');
	if ($rand_num <= 0)
	{
		header("Status: 400 参数num小于等于0");
		return;
	}
	if ($category_id)
	{
		$category = ORM::factory('category')->where('id', '=', $category_id)->find();
		if (!$category->loaded())
		{
			header("Status: 400 类别不存在");
			return;
		}
		$booklets = $category->booklets;
	}
	else
	{
		$booklets = ORM::factory('booklet');
	}
	switch ($lock)
	{
	case 0:
		$booklets->where('lock', '=', 'off');
		break;
	case 1:
		$booklets->where('lock', '=', 'on');
		break;
	default:
		$booklets->where('lock', '=', 'on');
		break;
	}
	$query_result = $booklets->find_all();
	$booklet_id_array = array();
	foreach ($query_result->as_array() as $booklet)
	{
		array_push($booklet_id_array, $booklet->id);
	}
	if (isset($booklet_id_array) AND !empty($booklet_id_array))
	{
		$query_result = ORM::factory('picture')->where('booklet_id', 'IN', $booklet_id_array)
			->order_by(DB::expr('RAND()'))->limit($rand_num)->find_all();
		$result = Book_Picture::instance()->create_picture_list($query_result->as_array());
		$picture = ORM::factory('rand_record')->create_record();
		json_Output::write($result);
	}
}

public function action_image()
{
	$picture_id = arr::get($_GET, 'picture_id');
	if ($picture_id)
    {
		$picture = ORM::factory('picture')->where('id', '=', $picture_id)->find();
		if ($picture->loaded())
		{
			Image_Output::write(Kohana::$config->load('upload')->get('picture').$picture->image);
		}
		else
		{
			header("Status: 400 图片id不存在");
			return;
		}
	}
	else
	{
		header("Status: 400 参数为空");
		return;
	}
}

public function action_thumbnail()
{
	$picture_id = arr::get($_GET, 'picture_id');
	if ($picture_id)
    {
		$picture = ORM::factory('picture')->where('id', '=', $picture_id)->find();
		if ($picture->loaded())
		{
			Book_Picture::instance()->render_thumb($picture->image);
		}
		else
		{
			header("Status: 400 图片id不存在");
			return;
		}
	}
	else
	{
		header("Status: 400 参数为空");
		return;
	}
}

}




